local UIManager = { }
local UIDefine = require "UICore.uiDefine"

UIManager.NtfMsgType = UIDefine.NtfMsgType
UIManager.ControllerName = UIDefine.ControllerName
UIManager.ViewName = UIDefine.ViewName
UIManager.LocalDataType = UIDefine.LocalDataType
UIManager.PopupStyle = UIDefine.PopupStyle

UIManager.Controller = require "UICore.Controller":extend()
UIManager.SubController = require "UICore.SubController":extend()
UIManager.View = require "UICore.View":extend()

local facade = require "UICore.Facade"

local rootCanvasTransform = nil

-- helper
local viewResource = {}

local function loadResource(path)
    local res = nil

    if viewResource[path] ~= nil then
        res = viewResource[path].resource
        viewResource[path].count = viewResource[path].count + 1
    else
        res = ResourceLoader.LoadAsset(path, callback)
        local info = Class()
        info.resource = res
        info.count = 1
        viewResource[path] = info
    end

    return res
end

local function unloadResource(path)
    if viewResource[path] ~= nil then
        local info = viewResource[path]
        if info.count == 0 then
            ResourceLoader.UnloadUI(path)
        end
    end
end

-- API

function UIManager.openController(name, data)
    facade:openController(name, data)
end

-- call ctrl:destroy, will
-- poping ctrl from stack
-- call sub ctrl:destroy
-- call self:onDestroy
-- call self:View():onDestroy
-- isOpen = false, isShow = false, view = nil, timerCd = nil
function UIManager.removeController(name)
    facade:removeController(name)
end

function UIManager.sendNtfMessage(ntfType, ...)
    facade:sendNtfMessage(ntfType, ...)
end

-- usually called by Controller instance ctor
function UIManager.registerController(controller)
    facade:registerController(controller)
end

function UIManager.controllerIsOpen(name)
    local ctrl = facade:getController(name)
    if nil == ctrl then
        return false
    else
        return ctrl.IsOpen
    end
end

function UIManager.pushingStack(ctrl)
    facade:pushingStack(ctrl)
end

function UIManager.popingStack(ctrl)
    facade:popingStack(ctrl)
end

function UIManager.createView(path, fileName, panelName, isFull)
    local res = loadResource(path)
    local ui = GameObject.Instantiate (res)

    if rootCanvasTransform == nil then
        local go = GameObject()
        go.name = "GlobalCanvas"
        go:AddComponent(typeof(CS.UnityEngine.RectTransform))
        local canvasComponent = go:AddComponent(typeof(CS.UnityEngine.Canvas))
        canvasComponent.renderMode = 0
        go:AddComponent(typeof(CS.UnityEngine.UI.CanvasScaler))
        go:AddComponent(typeof(CS.UnityEngine.UI.GraphicRaycaster))

        rootCanvasTransform = go.transform

        go = GameObject()
        go.name = "GlobalEventSystem"
        go:AddComponent(typeof(CS.UnityEngine.EventSystem))
        go:AddComponent(typeof(CS.UnityEngine.EventSystems.StandaloneInputModule))
    end

    ui.transform:SetParent(rootCanvasTransform, false)
    return ui, path
end

function UIManager.disposeView(path)
    if path ~= nil then

    end
end

-- framework functions
function UIManager.initialize()
    -- UIManager.openController(UIDefine.ControllerName.MainMenuPanel, nil)
end

function UIManager.update()

end

function UIManager.onDestroy()

end

return UIManager